# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: 单例模式练习.py
Author: kij
Create Date: 2021/2/18
-------------------------------------------------
"""


# 练习单例模式
class Mysql:
	__instance = None

	def __init__(self,name):
		self.name = name
		print("构造函数")

	@classmethod
	def singleton(cls,name):
		if not cls.__instance:
			obj = cls(name)  # 将类实例化
			cls.__instance = obj  # 将类对象赋值给__instance属性
		return cls.__instance  # 返回__instance这个类属性，相当于类的实例化对象

	def show_result(self):
		print("啥and啥")


mysql = Mysql.singleton('kij')
mysql1 = Mysql.singleton('kijlee')
mysql2 = Mysql.singleton('kijle')

# 地址不同
print(id(mysql))  # 140700530786256
print(mysql.name)

print(id(mysql1))  # 140700530786064
print(mysql1.name)

print(id(mysql2))  # 140700530786016 都是kij
print(mysql2.name)
mysql.name = 'kijlee'
print(mysql2.name)  # 变成了kijlee
